#include "websocketclient.h"

#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>

WebSocketClient::WebSocketClient(QObject *parent)
    : QObject{parent}
{
    m_webSocket = new QWebSocket;

    connect(m_webSocket, SIGNAL(connected()), this, SLOT(slot_connected()));
    connect(m_webSocket, SIGNAL(disconnected()), this, SLOT(slot_disconnected()));
}

void WebSocketClient::connectUrl(QString url)
{
    m_url = QUrl(url);
    m_webSocket->open(m_url);
}

void WebSocketClient::sendTextMessage(const QString &message)
{
    qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "send message =" << message;
    m_webSocket->sendTextMessage(message);
}

void WebSocketClient::close()
{
    m_isSigningOut = true;
    m_webSocket->close();
}

void WebSocketClient::slot_connected()
{
    qDebug() << __FILE__ << __FUNCTION__;
    m_isConnected = true;

    connect(m_webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(slot_recvTextMessage(QString)));
    connect(m_webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(slot_receiveTextMessage(QString)));
}

void WebSocketClient::slot_disconnected()
{
    qDebug() << __FILE__ << __LINE__ << __FUNCTION__;
    if (!m_isSigningOut) {
        reconnect();
    }
}

void WebSocketClient::slot_recvTextMessage(QString message)
{
//    qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "message =" << message;
    QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toStdString().data());
    QJsonObject jsonObject = jsonDocument.object();
//    qDebug() << __FUNCTION__ << "data =" << jsonObject.value("data").toString();
    emit signal_recvTextMessage(jsonObject.value("data").toString());
}

void WebSocketClient::slot_receiveTextMessage(QString messageString)
{
    qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "receive messageString =" << messageString;

    QJsonParseError jsonParseError;
//    QJsonDocument jsonDocument = QJsonDocument::fromJson(messageString.toUtf8(), &jsonParseError);
    QJsonDocument jsonDocument = QJsonDocument::fromJson(messageString.toStdString().data(), &jsonParseError);
    if (jsonParseError.error != QJsonParseError::NoError) {
        qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << jsonParseError.errorString();
        return;
    }

    if (!jsonDocument.isObject())
    {
        qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "!jsonDocument.isObject()";
        return;
    }

    QJsonObject jsonObject = jsonDocument.object();

    Message message;
    message.setType(jsonObject.value("type").toString());
    message.setFromId(jsonObject.value("fromId").toString());

    switch (message.type()) {
    case Message::SINGAL_CHAT:
        message.setToId(jsonObject.value("toId").toString());
        break;
    case Message::GROUP_CHAT:
        message.setGroupId(jsonObject.value("groupId").toString());
        break;
    case Message::FRIEND_REQUEST:
        message.setToId(jsonObject.value("toId").toString());
        break;
    default:
        break;
    }

    message.setData(jsonObject.value("data").toString());
    message.setTimestamp(QDateTime::fromString(jsonObject.value("timestamp").toString(), "yyyy-MM-dd hh:mm:ss"));

    qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "message =" << message.getMessage();
    emit signal_receiveTextMessage(message);
}

void WebSocketClient::reconnect()
{
    qDebug() << __FUNCTION__ << "WebSocket reconnect";
    m_webSocket->abort();
    m_webSocket->open(m_url);
}
